﻿@using SelfMadeMillionaire.Core.Enums
@using SelfMadeMillionaire.Web.Infrastructure
@using SelfMadeMillionaire.Web.Infrastructure.Formatters
@using SelfMadeMillionaire.Web.Infrastructure.ViewModels.Newsletters
@using PagedList.Mvc
@model NewsletterListViewModel

<div class="table-responsive">
		@if (Model.Newsletters.Any())
  {
			<table class="table table-bordered tablesorter">
				<thead>
					<tr>
						<th width="15%"><a href="#">Date</a></th>
						<th width="15%"><a href="#">Issue Type</a></th>
						<th width="15%"><a href="#">Symbols</a></th>						
						<th width="25%"><a href="#">Letter</a></th>
						<th><a href="#">Title</a></th>
					</tr>
				</thead>
				<tbody>
					@foreach (var item in Model.Newsletters)
	 {
						<tr>
							<td>@item.Date.ToString("MM/dd/yyyy")</td>
							<td>@item.IssueType.Title</td>
							<td>@item.Symbols</td>
							<td>@ProductTypeFormatter.GetProductTypeName((ProductType)item.Letter)</td>
							<td><a href="@Url.Action("Details", "Newsletters", new { id = item.ID, area = "" })">@item.Title</a></td>							
						</tr>
	 }
				</tbody>
			</table>
	  @Html.PagedListPager(Model.Newsletters, page => Url.Action("Index", new { page, startDate = Model.StartDate, endDate = Model.EndDate, selectedProduct = Model.ProductTypes.SelectedValue, selectedIssueType = Model.IssueTypes.SelectedValue, symbols = Model.Symbols,  title = Model.Title, sort = Model.SortOrder }))
  }
  else
  {
			<p>No records found.</p>
  }
	</div>

<script type="text/javascript">
	$(function () {

		$.tablesorter.addParser({
			id: 'titles',
			is: function (s) {
				return false;
			},
			format: function (s) {
				return s;
			},
			type: 'text'
		});
		$.tablesorter.addParser({
			id: 'symbols',
			is: function (s) {
				return false;
			},
			format: function (s) {
				return s;
			},
			type: 'text'
		});
		$("#newsletters-list table").tablesorter({
		    headers: {
		        2: {
		            sorter: 'symbols'
		        },
		        4: {
		            sorter: 'titles'
		        }
				
		    }
		});
		
	    /*$("#newsletters-list table").bind("sortStart", function (event) {
	        var table = event.target;
	    })*/

	    var sortRefresh = true;
        @if(!string.IsNullOrEmpty(Model.SortOrder))
        {
            <text>
	        sortRefresh = false;
	        setInitSort("newsletters-list", '@(Model.SortOrder)');
            </text>
        }

	    $("#newsletters-list table").on("sortEnd", function (event) {
	        $("#SortOrder").val(event.target.config.sortList);
	        if (sortRefresh)
	            RefreshList();
	        else
	            sortRefresh = true;

	    });
	});
</script>